package com.zhaoxi.project.wms.service;

import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.project.wms.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 跟物品有关的订单Service业务层处理
 *
 * @author zhaoxi
 * @date 2023-07-22
 */
@Service
public class OrderService extends BaseService {
  @Autowired
  private OrderMapper orderMapper;

  //查询数据集合
  public List<PageData> getList(PageData pd) {
    return orderMapper.getList(pd);
  }

  //查询数据对象 通过id
  public PageData getById(PageData pd) {
    PageData pd1 = new PageData();
    if (pd.getLong("id") == null){
      return null;
    }
    pd1.put("id",pd.getLong("id"));
    List<PageData> list = orderMapper.getList(pd);
    if (list.size() > 0){
      return list.get(0);
    }
    return null;
  }

  //查询数据行数
  public PageData getCount(PageData pd) {
    PageData res = new PageData();
    return this.success(orderMapper.getCount(pd));
  }

  //新增
  @Transactional
  public PageData add(PageData pd) {
    PageData res = new PageData();
//    获取返回的主键id
    int row = orderMapper.add(pd);
    if (row == 0){
      return this.success(row);
    }
    List<PageData> list = pd.getListPd("line");
    int i = 0;
    while (list.size() > i){
      PageData pd1 = list.get(i);
      if (pd1.getLong("id") == null) {
        pd1.put("id",pd.getLong("id"));
        this.addLine(pd1);
      } else {
        this.editLine(pd1);
      }
      i++;
    }
    return this.success(row);
  }

  //删除
  @Transactional
  public PageData edit(PageData pd) {
    int row = orderMapper.edit(pd);
    if (row == 0){
      return this.success(row);
    }
    Long id = pd.getLong("id");
    List<PageData> list = pd.getListPd("line");
    if (list == null){
      return this.success("订单修改成功");
    }
    for (PageData pd1 : list){
      if (pd1.getLong("id") == null) {
        pd1.put("id",id);
        this.addLine(pd1);
      } else {
        this.editLine(pd1);
      }
    }
    return this.success(row);
    
  }

  //删除
  public PageData del(Long id) {
    PageData res = new PageData();
    res.put("id",id);
    this.delLine(res);
    return this.success(orderMapper.del(id));
    
  }


  //子表数据的增删改查================================
  //查询数据集合
  public List<PageData> getLineList(PageData pd) {
    return orderMapper.getLineList(pd);
  }

  //查询数据行数
  public PageData getLineCount(PageData pd) {
    PageData res = new PageData();
    return this.success(orderMapper.getLineCount(pd));
    
  }

  //新增
  public PageData addLine(PageData pd) {
    PageData res = new PageData();
    return this.responses(orderMapper.addLine(pd));
  }

  //删除
  public PageData editLine(PageData pd) {
    return this.responses(orderMapper.editLine(pd));
    
  }

  //删除
  public PageData delLine(PageData pd) {
    return this.responses(orderMapper.delLine(pd));
    
  }

  //  =================tree关联表的数据处理
//查询数据集合
  public List<PageData> getTypeList(PageData pd) {
    List<PageData> list = orderMapper.getTypeList(pd);
    return orderMapper.getTypeList(pd);
  }

  //查询数据对象 通过id
  public PageData getTypeById(PageData pd) {
    PageData pd1 = new PageData();
    if (pd.getLong("typeId") == null){
      return null;
    }
    pd1.put("typeId",pd.getLong("typeId"));
    List<PageData> list = orderMapper.getTypeList(pd1);
    if (list.size() > 0){
      return list.get(0);
    }
    return null;
  }

  //查询数据行数
  public PageData getTypeCount(PageData pd) {
    return this.success(orderMapper.getTypeCount(pd));
    
  }

  //新增
  public PageData addType(PageData pd) {
    return this.responses(orderMapper.addType(pd));
    
  }

  //删除
  public PageData editType(PageData pd) {
    PageData res = new PageData();
    return this.responses(orderMapper.editType(pd));
    
  }

  //删除
  public PageData delType(Long typeId) {
    PageData res = new PageData();
    return this.responses(orderMapper.delType(typeId));
    
  }
}
